package com.eden.api.controller;

import com.eden.api.controller.vo.ApiResult;
import com.eden.common.utils.StringUtils;
import com.eden.websocket.domain.ChatList;
import com.eden.websocket.service.IChatListService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

import static com.eden.api.controller.vo.ApiResult.success;

/**
 * @program: eden
 * @description: 消息列表客户端控制层
 * @author: Mr.Jiao
 * @create: 2021-03-06 17:04
 */
@Slf4j
@Api("消息列表客户端")
@RestController
@RequestMapping("/api/chatList")
public class ApiChatListController {

    @Autowired
    private IChatListService chatListService;

    //查询消息列表
    @ApiOperation("加载消息列表")
    @PostMapping("/selectChatList")
    public ApiResult selectChatList(@Validated @RequestBody ChatList chatList) {
        List<ChatList> chatLists = chatListService.selectChatListList(chatList);
        return success("查询成功！！", chatLists);
    }

    //添加消息列表
    @ApiOperation("添加消息列表")
    @PostMapping("/addChatList")
    public ApiResult addChatList(@Validated @RequestBody ChatList chatList) {
        //添加消息列表需要同时添加两条己方和他方
        ChatList toUser = new ChatList();
        List<ChatList> lists = chatListService.selectChatListList(chatList);
        if (lists.size() == 0) {
            if (StringUtils.isNotNull(chatList)
                    && StringUtils.isNotEmpty(chatList.getChatToLoginName())
                    && StringUtils.isNotEmpty(chatList.getChatFromLoginName())
                    && StringUtils.isNotEmpty(chatList.getChatFromUserName())
                    && StringUtils.isNotEmpty(chatList.getFromUserAvatar())) {
                //添加进入发起方聊天列表
                chatListService.insertChatList(chatList);
                //添加进入收消息方聊天列表
                toUser.setChatFromLoginName(chatList.getChatToLoginName());
                toUser.setChatToLoginName(chatList.getChatFromLoginName());
                toUser.setChatToUserName(chatList.getChatFromUserName());
                toUser.setToUserAvatar(chatList.getFromUserAvatar());
                chatListService.insertChatList(toUser);
            }
        } else {
            return success("消息列表已存在无需添加");
        }
        return success("添加消息列表成功！！");
    }

    //删除消息列表
    @ApiOperation("加载消息列表")
    @PostMapping("/delChatList")
    public ApiResult delChatList(@Validated @RequestBody ChatList chatList) {
        chatListService.deleteChatList(chatList);
        return success("删除消息列表成功！！");
    }

    //更新到消息列表
    @ApiOperation("更新到消息列表")
    @PostMapping("/updateChatList")
    public ApiResult updateChatList(@Validated @RequestBody ChatList chatList) {
        chatList.setNoReadCount(0);
        chatListService.updateChatList(chatList);
        return success("更新消息列表成功！！");
    }

}
